import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public class StandardFrame extends JFrame {

	// global variables
	private final int offsetBoard = 20;
	private final int offsetButtonsX = 550;
	private final int buttonXDim = 80;
	private final int buttonYDim = 50;
	private final int pegSize = 50;
	private final int numberOfPegs = 9;
	private static int count = 0;
	// peg counter
	private int t = 0;
	
	static boolean position[] = new boolean[24];

	// position occupy
	static void initalPosition() {
		for (int j = 0; j < 24; j++) {
			position[j] = false;
		}
	}

	// determine values and action on board
	private JLabel statusbar;
	private JLabel counter;
	private JPanel mousepanel;

	// set of buttons
	private JButton save;
	private JButton load;
	private JButton undo;
	private JButton hint;
	private JButton exit;

	// holds insets values of main frame
	private Insets insets;

	// holds the board image
	private JLabel board;

	// holds the peg images
	private JLabel[] black = new JLabel[18];
	private JLabel black2;
	private JLabel black3;
	private JLabel white;

	// constructor
	public StandardFrame() {
		// title
		super("Nine Mens Morris");
		setLayout(null);

		// sets insets of main frame
		insets = getInsets();

		// add a save handler to save the game
		// places the save button
		SaveHandler saveHandler = new SaveHandler();
		save = new JButton("save");
		save.setBounds(insets.right + offsetButtonsX, insets.top + 10, buttonXDim, buttonYDim);
		save.addMouseListener(saveHandler);
		save.addMouseMotionListener(saveHandler);
		add(save);

		// add a load handler to load the game
		LoadHandler loadHandler = new LoadHandler();
		// places the load button
		load = new JButton("load");
		load.setBounds(insets.right + offsetButtonsX, insets.top + 1* buttonYDim + 20, buttonXDim, buttonYDim);
		load.addMouseListener(loadHandler);
		load.addMouseMotionListener(loadHandler);
		add(load);

		// add a undo handler to undo the game
		UndoHandler undoHandler = new UndoHandler();
		// places the undo button
		undo = new JButton("undo");
		undo.setBounds(insets.right + offsetButtonsX, insets.top + 2* buttonYDim + 30, buttonXDim, buttonYDim);
		undo.addMouseListener(undoHandler);
		undo.addMouseMotionListener(undoHandler);
		add(undo);

		// add a hint handler to hint the game
		HintHandler hintHandler = new HintHandler();
		// places the hint button
		hint = new JButton("hint");
		hint.setBounds(insets.right + offsetButtonsX, insets.top + 3* buttonYDim + 40, buttonXDim, buttonYDim);
		hint.addMouseListener(hintHandler);
		hint.addMouseMotionListener(hintHandler);
		add(hint);

		// declare exit handler class
		ExitHandler exitHandler = new ExitHandler();

		// places the exit button
		exit = new JButton("exit");
		exit.setBounds(insets.right + offsetButtonsX, insets.top + 4* buttonYDim + 50, buttonXDim, buttonYDim);
		exit.addMouseListener(exitHandler);
		exit.addMouseMotionListener(exitHandler);
		add(exit);

// REMOVE FOR FINAL VERSION
		// adding mousepanel and statusbar
		mousepanel = new JPanel();
		mousepanel.setBounds(insets.right + offsetButtonsX, insets.top + 8* buttonYDim, 80, 80);
		add(mousepanel);

		statusbar = new JLabel();
		statusbar.setBounds(insets.right + offsetButtonsX, insets.top + 9* buttonYDim, 200, 100);
		add(statusbar);
		
		counter = new JLabel();
		counter.setBounds(insets.right + offsetButtonsX, insets.top + 10* buttonYDim, 200, 100);
		add(counter);
		
// REMOVE FOR FINAL VERSION

		// mouse handler creator
		BoardHandler bHandler = new BoardHandler();

		// creating the peg icons
		ImageIcon blackPeg = new ImageIcon("src/peg_black.png");
		ImageIcon whitePeg = new ImageIcon("src/peg_white.png");
		for(int i = 0; i < 9; i ++)
			black[i] = new JLabel(blackPeg);
		for(int i = 9; i < 18; i++)
			black[i] = new JLabel(whitePeg);
		white = new JLabel(whitePeg);
		black2 = new JLabel(blackPeg);
 		black3 = new JLabel(blackPeg);
		

		// creates the board image
		ImageIcon img = new ImageIcon("src/image.jpg");
		int xcord = img.getIconWidth();
		int ycord = img.getIconHeight();
		board = new JLabel(img);

		// add the board to main frame
		// adds mouse handlers to board
		board.setBounds(insets.right + offsetBoard, insets.top + offsetBoard,xcord, ycord);
		board.addMouseListener(bHandler);
		board.addMouseMotionListener(bHandler);
		add(board);

		repaint();

	}

	// mouse handler
	// has added in statusbar and mousepanel to determine values on board -->
	// needs to be removed in final version
	private class BoardHandler implements MouseListener, MouseMotionListener {

		// when mouse is clicked place peg in players color
		public void mouseClicked(MouseEvent event) {
			// currentPeg used to get the current peg color
			JLabel currentPeg = new JLabel();
			currentPeg = white;
								
			// get to know who place the peg, the white or the black
			int remainder = StandardFrame.getCount() % 2;
			if (remainder != 0){
				currentPeg = black[getCount()-1];
			}
			statusbar.setText(String.format("Clicked at %d, %d", event.getX(),
											event.getY()));
			PlacePeg(event, currentPeg);		
		}

		// no action required for all other mouse events
		public void mousePressed(MouseEvent event) {
			statusbar.setText("pressed mouse");
		}

		public void mouseReleased(MouseEvent event) {
			statusbar.setText("released mouse");
		}

		public void mouseEntered(MouseEvent event) {
			statusbar.setText("entered area");
			mousepanel.setBackground(Color.RED);
		}

		public void mouseExited(MouseEvent event) {
			statusbar.setText("left area");
			mousepanel.setBackground(Color.WHITE);
		}

		public void mouseDragged(MouseEvent event) {
			// currentPeg used to get the current peg color
			JLabel currentPeg = new JLabel();
			currentPeg = white;
					
			// get to know who place the peg, the white or the black
			int remainder = StandardFrame.getCount() % 2;
			if (remainder != 0){
				currentPeg = black[getCount()];
			}
			statusbar.setText(String.format("Clicked at %d, %d", event.getX(),
								event.getY()));
			PlacePeg(event, currentPeg);			
			
		}

		public void mouseMoved(MouseEvent event) {
			statusbar.setText("moved mouse");
		}

	}

	private class ExitHandler implements MouseListener, MouseMotionListener {

		// when mouse is clicked place peg in players color
		public void mouseClicked(MouseEvent event) {
			System.exit(0);
		}

		// no action required for all other mouse events
		public void mousePressed(MouseEvent event) {
		}

		public void mouseReleased(MouseEvent event) {

		}

		public void mouseEntered(MouseEvent event) {

		}

		public void mouseExited(MouseEvent event) {

		}

		public void mouseDragged(MouseEvent event) {

		}

		public void mouseMoved(MouseEvent event) {

		}

	}

	private class LoadHandler implements MouseListener, MouseMotionListener {

		// when mouse is clicked place peg in players color
		public void mouseClicked(MouseEvent event) {
			
		}

		// no action required for all other mouse events
		public void mousePressed(MouseEvent event) {
		}

		public void mouseReleased(MouseEvent event) {

		}

		public void mouseEntered(MouseEvent event) {

		}

		public void mouseExited(MouseEvent event) {

		}

		public void mouseDragged(MouseEvent event) {

		}

		public void mouseMoved(MouseEvent event) {

		}

	}

	private class UndoHandler implements MouseListener, MouseMotionListener {

		// when mouse is clicked place peg in players color
		public void mouseClicked(MouseEvent event) {
			System.exit(0);
		}

		// no action required for all other mouse events
		public void mousePressed(MouseEvent event) {
		}

		public void mouseReleased(MouseEvent event) {

		}

		public void mouseEntered(MouseEvent event) {

		}

		public void mouseExited(MouseEvent event) {

		}

		public void mouseDragged(MouseEvent event) {

		}

		public void mouseMoved(MouseEvent event) {

		}

	}

	private class HintHandler implements MouseListener, MouseMotionListener {

		// when mouse is clicked place peg in players color
		public void mouseClicked(MouseEvent event) {
			System.exit(0);
		}

		// no action required for all other mouse events
		public void mousePressed(MouseEvent event) {
		}

		public void mouseReleased(MouseEvent event) {

		}

		public void mouseEntered(MouseEvent event) {

		}

		public void mouseExited(MouseEvent event) {

		}

		public void mouseDragged(MouseEvent event) {

		}

		public void mouseMoved(MouseEvent event) {

		}

	}

	private class SaveHandler implements MouseListener, MouseMotionListener {

		// when mouse is clicked place peg in players color
		public void mouseClicked(MouseEvent event) {
			
		}

		// no action required for all other mouse events
		public void mousePressed(MouseEvent event) {
		}

		public void mouseReleased(MouseEvent event) {

		}

		public void mouseEntered(MouseEvent event) {

		}

		public void mouseExited(MouseEvent event) {

		}

		public void mouseDragged(MouseEvent event) {

		}

		public void mouseMoved(MouseEvent event) {

		}

	}

	// method that find and places the peg in the clicked spot
	// need to add the correct peg color (?? getPlayer() ??)
	public void PlacePeg(MouseEvent event, JLabel peg) {
		
		// board numbering from left to right starting at 1
		// places the players peg

		// row 1
		// position 1
		if ((event.getX() > 20 && event.getX() < 40)
				&& (event.getY() > 20 && event.getY() < 40)) {
			
			this.DoPlacePeg(event, 1, peg);
		}

		// position 2
		if ((event.getX() > 210 && event.getX() < 230)
				&& (event.getY() > 20 && event.getY() < 40)) {
			
			this.DoPlacePeg(event, 2, peg);
		}
		// position 3
		if ((event.getX() > 390 && event.getX() < 410)
				&& (event.getY() > 20 && event.getY() < 40)) {
			
			this.DoPlacePeg(event, 3, peg);
		}

		// row 2
		// position 4
		if ((event.getX() > 80 && event.getX() < 100)
				&& (event.getY() > 80 && event.getY() < 100)) {
			
			this.DoPlacePeg(event, 4, peg);
		}
		// position 5
		if ((event.getX() > 210 && event.getX() < 230)
				&& (event.getY() > 80 && event.getY() < 100)) {
			
			this.DoPlacePeg(event, 5, peg);
		}
		// position 6
		if ((event.getX() > 335 && event.getX() < 355)
				&& (event.getY() > 80 && event.getY() < 100)) {
			
			this.DoPlacePeg(event, 6, peg);
		}

		// row 3
		// position 7
		if ((event.getX() > 130 && event.getX() < 150)
				&& (event.getY() > 130 && event.getY() < 150)) {
			
			this.DoPlacePeg(event, 7, peg);
		}
		// position 8
		if ((event.getX() > 210 && event.getX() < 230)
				&& (event.getY() > 130 && event.getY() < 150)) {
			
			this.DoPlacePeg(event, 8, peg);
		}
		// position 9
		if ((event.getX() > 280 && event.getX() < 300)
				&& (event.getY() > 130 && event.getY() < 150)) {
			
			this.DoPlacePeg(event, 9, peg);
		}

		// row 4 (6 pegs)
		// position 10
		if ((event.getX() > 20 && event.getX() < 40)
				&& (event.getY() > 205 && event.getY() < 225)) {
			
			this.DoPlacePeg(event, 10, peg);
		}
		// position 11
		if ((event.getX() > 80 && event.getX() < 100)
				&& (event.getY() > 205 && event.getY() < 225)) {
			
			this.DoPlacePeg(event, 11, peg);
		}
		// position 12
		if ((event.getX() > 130 && event.getX() < 150)
				&& (event.getY() > 205 && event.getY() < 225)) {
			
			this.DoPlacePeg(event, 12, peg);
		}
		// position 13
		if ((event.getX() > 285 && event.getX() < 305)
				&& (event.getY() > 205 && event.getY() < 225)) {
			
			this.DoPlacePeg(event, 13, peg);
		}
		// position 14
		if ((event.getX() > 335 && event.getX() < 355)
				&& (event.getY() > 205 && event.getY() < 225)) {
			
			this.DoPlacePeg(event, 14, peg);
		}
		// position 15
		if ((event.getX() > 390 && event.getX() < 410)
				&& (event.getY() > 205 && event.getY() < 225)) {
			
			this.DoPlacePeg(event, 15, peg);
		}

		// row 5
		// position 16
		if ((event.getX() > 130 && event.getX() < 150)
				&& (event.getY() > 290 && event.getY() < 310)) {
			
			this.DoPlacePeg(event, 16, peg);
		}
		// position 17
		if ((event.getX() > 210 && event.getX() < 230)
				&& (event.getY() > 290 && event.getY() < 310)) {
			
			this.DoPlacePeg(event, 17, peg);
		}
		// position 18
		if ((event.getX() > 280 && event.getX() < 300)
				&& (event.getY() > 290 && event.getY() < 310)) {
			
			this.DoPlacePeg(event, 18, peg);
		}

		// row 6
		// position 19
		if ((event.getX() > 80 && event.getX() < 100)
				&& (event.getY() > 335 && event.getY() < 355)) {
			
			this.DoPlacePeg(event, 19, peg);
		}
		// position 20
		if ((event.getX() > 210 && event.getX() < 230)
				&& (event.getY() > 335 && event.getY() < 355)) {
			
			this.DoPlacePeg(event, 20, peg);
		}
		// position 21
		if ((event.getX() > 335 && event.getX() < 355)
				&& (event.getY() > 335 && event.getY() < 355)) {
			
			this.DoPlacePeg(event, 21, peg);
		}

		// row 7
		// position 22
		if ((event.getX() > 20 && event.getX() < 40)
				&& (event.getY() > 390 && event.getY() < 410)) {

			
			this.DoPlacePeg(event, 22, peg);
		}
		// position 23
		if ((event.getX() > 210 && event.getX() < 230)
				&& (event.getY() > 390 && event.getY() < 410)) {
			
			this.DoPlacePeg(event, 23, peg);
		}
		// position 24
		if ((event.getX() > 390 && event.getX() < 410)
				&& (event.getY() > 390 && event.getY() < 410)) {
			
			this.DoPlacePeg(event, 24, peg);
		}

		

	}

	public static int getCount() {
		return count;
	}

	public static void setCount(int count) {
		StandardFrame.count = count;
	}

	public void DoPlacePeg (MouseEvent event, int i, JLabel peg) {
		
		if (position[i-1] == false) {
			peg.setBounds(event.getX() - (pegSize / 2), event.getY()
					- (pegSize / 2), pegSize, pegSize + 1);
			board.add(peg);
			position[i-1] = true;
			setCount(getCount() + 1);
			counter.setText(String.format("Count %d", getCount()));
			
		}
	}
}
